package com.uchengwang.ui.cm_ui.me.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.functions.http.listener.OnHttpResultListener;
import com.uchengwang.functions.http.listener.OnHttpResultSingleListener;
import com.uchengwang.functions.image.ImageManager;
import com.uchengwang.ui.UChengApplication;
import com.uchengwang.ui.base_ui.ui.BaseNoTitleActivity;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleBean;
import com.uchengwang.ui.cm_ui.cycle.bean.CycleShareBean;
import com.uchengwang.ui.cm_ui.cycle.http.CycleHttpUtils;
import com.uchengwang.ui.cm_ui.cycle.ui.LoadCycleWebHtmlActivity;
import com.uchengwang.ui.cm_ui.login.bean.UserBean;
import com.uchengwang.ui.cm_ui.me.adapter.CirclePageAdapter;
import com.uchengwang.ui.cm_ui.me.presenter.HttpMeManager;
import com.uchengwang.ui.utils.ChangeTextColorUtils;
import com.uchengwang.ui.utils.SPUtils;
import com.uchengwang.ui.utils.StatusBarUtil;
import com.uchengwang.ui.utils.UCommonUtil;
import com.uchengwang.ui.view.ExpandTextView;

import java.util.List;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2018/1/22.
 * 我的或者他人主页
 */

public class MainPageActivity extends BaseNoTitleActivity implements View.OnClickListener{
    private ImageView head, back, more, sex, releaseItem_img, userInfo_attenImage;
    private TextView name, location, releaseItem_theme, releaseItem_content, userInfo_attenText, userInfo_divider;
    private RelativeLayout quan_rl, release_rl, myMain_back_rl;
    private RecyclerView rv;
    private ExpandTextView sign;

    private LinearLayout bottom_atten, bottom_chat, releaseItem_ln;//底部的关注/聊天

    private long uid;//自己/对方的uid
    private UserBean userBean;
    private CirclePageAdapter adapter;
    private TextView noCycle, noShare;
    private CycleShareBean shareBean;//别人的发布

    private boolean isFriend, isFollow;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        isInitMode = false;
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_my_main);
        StatusBarUtil.getUtil().setTransparencyBar(this);

        uid = getIntent().getLongExtra(BaseConfig.ID, 0);
        userBean = (UserBean) getIntent().getSerializableExtra(BaseConfig.BEAN);

        initView();
        initData();
    }

    private void initView(){
        myMain_back_rl = (RelativeLayout) findViewById(R.id.myMain_back_rl);
        head = (ImageView) findViewById(R.id.myMain_head);//头像
        back = (ImageView) findViewById(R.id.myMain_back);//返回键
        back.setOnClickListener(this);
        more = (ImageView) findViewById(R.id.myMain_more);//更多操作
        more.setOnClickListener(this);
        name = (TextView) findViewById(R.id.myMain_name);//名字

        sex = (ImageView) findViewById(R.id.myMain_sex);//性别
        location = (TextView) findViewById(R.id.myMain_location);//大学

        sign = (ExpandTextView) findViewById(R.id.myMain_sign);//标语

        quan_rl = (RelativeLayout) findViewById(R.id.myMain_quan_rl);//关联社圈
        quan_rl.setOnClickListener(this);

        rv = (RecyclerView) findViewById(R.id.myMain_rv);//关联的社圈的数据
        release_rl = (RelativeLayout) findViewById(R.id.myMain_release_rl);//他的发布
        release_rl.setOnClickListener(this);

        noCycle = (TextView) findViewById(R.id.myMain_noCycle);//没有关注社圈时


        noShare = (TextView) findViewById(R.id.myMain_noShare);//没有发布信息时

        releaseItem_ln = (LinearLayout) findViewById(R.id.releaseItem_ln);
        releaseItem_ln.setOnClickListener(this);
        releaseItem_img = (ImageView) findViewById(R.id.releaseItem_img);//他的发布的图标
        releaseItem_theme = (TextView) findViewById(R.id.releaseItem_theme);//他的发布的主题
        releaseItem_theme.setOnClickListener(this);
        releaseItem_content = (TextView) findViewById(R.id.releaseItem_content);//信息内容

        bottom_atten = (LinearLayout) findViewById(R.id.userInfo_bottom_atten);//底部关注
        userInfo_attenImage = (ImageView) findViewById(R.id.userInfo_attenImage);
        userInfo_attenText = (TextView) findViewById(R.id.userInfo_attenText);
        bottom_atten.setOnClickListener(this);

        userInfo_divider = (TextView) findViewById(R.id.userInfo_divider);//分割线

        bottom_chat = (LinearLayout) findViewById(R.id.userInfo_bottom_chat);//底部聊天

        if (uid ==UCommonUtil.getUtil().getId()){
            noCycle.setOnClickListener(this);
            //自己的主页
            userInfo_divider.setVisibility(View.GONE);
            bottom_chat.setVisibility(View.GONE);
            ImageManager.getInstance().loadImage(this, userInfo_attenImage, R.drawable.icon_main_page_editor, ImageManager.ImageType.NARMOL);
            userInfo_attenText.setTextColor(getResources().getColor(R.color.selector_login_color));
            userInfo_attenText.setText("编辑资料");
        }else {
            //别人的主页
            userInfo_divider.setVisibility(View.GONE);
            bottom_chat.setVisibility(View.GONE);
        }

        if (userBean != null){
            initDataView();
        }else {
            //获取网络数据
            getMyInfo();
        }
    }

    private void initData(){
        adapter = new CirclePageAdapter(this);
        rv.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        rv.setAdapter(adapter);

        getMyCycle();
        getMyShare();
    }

    //初始化用户信息
    private void initDataView(){
        if (userBean != null && uid != UCommonUtil.getUtil().getId()){
            ImageManager.getInstance().loadImage(this, head, userBean.getUserAvatar(), ImageManager.ImageType.CIRCLE);
            name.setText(userBean.getNickName());
            location.setText(userBean.getUniName());
            if (!TextUtils.isEmpty(userBean.getUserSign())){
                sign.setText(userBean.getUserSign());
            }else {
                sign.setVisibility(View.GONE);
            }
            switch (userBean.getUserRole()){
                case BaseConfig.STUDENT_ROLE:
                    sex.setImageResource(userBean.getSex() ==0? R.drawable.icon_woman : R.drawable.icon_man);
                    break;
                default:
                    sex.setVisibility(View.GONE);
                    break;
            }
            isFriend = userBean.isFriend();
            isFollow = userBean.isFollow();

            if (isFriend ||isFollow){
                //是好友 已关注
                userInfo_attenImage.setVisibility(View.GONE);
                userInfo_attenText.setText("已关注");
                userInfo_attenText.setTextColor(getResources().getColor(R.color.delete_selector));
            }
        }
    }

    //从网络获取用户信息
    private void getMyInfo(){
        HttpMeManager.getPresenter().getUserInfo(this, uid, new OnHttpResultSingleListener<UserBean>() {
            @Override
            public void onResult(int status, String msg, UserBean bean) {
                userBean = bean;
                initDataView();
            }
        });
    }

    //获取我/别人关注的社圈
    private void getMyCycle(){
        CycleHttpUtils.getUtils().getFollowCircle(this, uid, 1, 999, 0, new OnHttpResultListener<CycleBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleBean> list) {
                if (list != null){
                    adapter.setData(list);
                }else {
                    rv.setVisibility(View.GONE);
                    noCycle.setVisibility(View.VISIBLE);
                    if(uid == UCommonUtil.getUtil().getId()){
                        String t = "您还没有关注过社圈, 赶紧去关注吧";
                        noCycle.setText(ChangeTextColorUtils.getUtils().changeColor(MainPageActivity.this.getResources().getColor(R.color.color_location), t, 11, 16));
                    }else {
                        String t = "Ta还未关注过社圈";
                        noCycle.setText(t);
                    }

                }
            }
        });
    }
    //获取我/别人的发布
    private void getMyShare(){
        CycleHttpUtils.getUtils().getShare(this, uid, 0, 1, 1, 0,  new OnHttpResultListener<CycleShareBean>() {
            @Override
            public void onResult(int status, String msg, int total, List<CycleShareBean> list) {
                if (list != null && list.size() >0){
                    shareBean = list.get(0);
                    ImageManager.getInstance().loadImage(MainPageActivity.this, releaseItem_img, shareBean.getShareFirstImg(), ImageManager.ImageType.ROUND);
                    if (!TextUtils.isEmpty(shareBean.getCycleName())){
                        releaseItem_theme.setText("#"+ shareBean.getCycleName() +"#");
                    }
                    releaseItem_content.setText(shareBean.getShareContent());
                }else {
                    releaseItem_ln.setVisibility(View.GONE);
                    noShare.setVisibility(View.VISIBLE);
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (uid ==UCommonUtil.getUtil().getId()){//自己的信息更改时
            ImageManager.getInstance().loadImage(this, head, UChengApplication.userAvatar, ImageManager.ImageType.CIRCLE);
            name.setText(UChengApplication.nickName);
            location.setText(UChengApplication.mySchoolName);

            String signS = SPUtils.getPreferences(this).getString(BaseConfig.USER_SIGN);
            if (!TextUtils.isEmpty(signS)){
                sign.setText(signS);
            }else {
                sign.setVisibility(View.GONE);
            }
            sex.setImageResource(UChengApplication.sex ==0? R.drawable.icon_woman : R.drawable.icon_man);
        }

    }

    @Override
    public void onClick(View v) {
        Intent intent = null;
        switch (v.getId()){
            case R.id.myMain_back://返回键
                finish();
                break;
            case R.id.myMain_more://更多操作
                UCommonUtil.getUtil().showToast(this, "more");
                break;
            case R.id.myMain_quan_rl://关注的更多社圈
                UCommonUtil.getUtil().showToast(this, "更多社圈");
                break;

            case R.id.myMain_noCycle://没有关注社圈时 点击去关注
                UCommonUtil.getUtil().showToast(this, "关注社圈");
                break;

            case R.id.myMain_release_rl://他的更多发布
                UCommonUtil.getUtil().showToast(this, "更多发布");
                intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/uCity/view/myInvitation/myInvitation.html?checkUid="+uid +"&isopen=true");
                break;

            case R.id.releaseItem_ln://点击进入一个发布的信息详情
                if (shareBean != null){
                    intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                    intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/uCity/view/invitationDetail/invitationDetail.html?id="+shareBean.getId()  +"&isopen=true");
                }

                break;

            case R.id.releaseItem_theme://他的一条的发布信息, 对应的社圈
                if (shareBean != null){
                    intent = new Intent(this, LoadCycleWebHtmlActivity.class);
                    intent.putExtra(BaseConfig.URL, BaseConfig.BaseWebURL + "/uCity/view/socialCircleDetail/socialCircle"+ shareBean.getCycleId()+".html"  +"?isopen=true");
                }
                break;

            case R.id.userInfo_bottom_atten:
                if (uid == UCommonUtil.getUtil().getId()){
                    //是自己 点击编辑资料
                    intent = new Intent(this, MyPerInfoActivity.class);
                }else {
                    if (userBean != null){
                        follow();//请求数据成功后
                    }
                }
                break;
        }
        if (intent != null){
            startActivity(intent);
        }
    }

    /**
     * 别人的界面 是否关注
     * */
    private void follow(){
        HttpMeManager.getPresenter().addAtten(this, !isFollow, uid, new HttpMeManager.AttenListener() {
            @Override
            public void atten(int status) {
                if (status == 0){
                    if (isFollow){
                        UCommonUtil.getUtil().showToast(MainPageActivity.this, "取消关注成功");
                        userInfo_attenText.setText("关注");
                        userInfo_attenImage.setVisibility(View.VISIBLE);
                        userInfo_attenText.setTextColor(getResources().getColor(R.color.register_user_selector));
                        isFollow = false;
                    }else {
                        UCommonUtil.getUtil().showToast(MainPageActivity.this, "关注成功");
                        userInfo_attenText.setText("已关注");
                        userInfo_attenImage.setVisibility(View.GONE);
                        userInfo_attenText.setTextColor(getResources().getColor(R.color.delete_selector));
                        isFollow = true;
                    }

                    if (userBean != null){
                        userBean.setFollow(isFollow);
                    }
                    EventBus.getDefault().post(userBean);

                }else {
                    UCommonUtil.getUtil().showToast(MainPageActivity.this, "操作失败");
                }
            }
        });
    }
}
